package com.sunshuo.springsecuritydemo.dao;

import com.sunshuo.springsecuritydemo.pojo.ResourceRoleResult;
import com.sunshuo.springsecuritydemo.pojo.Role;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * @ClassName RoleDao
 * @Description
 * @Author sunshuo
 * @Date 2021/11/7 20:24
 */
@Repository
public class RoleDao {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public List<Role> getRoles() {
        String sql  = "select * from t_role";
        List<Role> roles = jdbcTemplate.query(sql, new RowMapper<Role>() {
            @Override
            public Role mapRow(ResultSet rs, int rowNum) throws SQLException {
                Role role = new Role();
                role.setId(rs.getString("id"));
                role.setName(rs.getString("name"));
                role.setRemarks(rs.getString("remarks"));
                role.setDel(rs.getInt("is_del") == 1);
                return role;
            }
        });
        return roles;
    }

    public List<String> getRoleNameByUserId(String userId) {
        String sql = "select role.name from t_user_role user_role left join t_role role on user_role.role_id = role.id WHERE user_role.user_id = ?";
        List<String> result = jdbcTemplate.query(sql, new RowMapper<String>() {
            @Override
            public String mapRow(ResultSet rs, int rowNum) throws SQLException {
                return rs.getString("name");
            }
        }, userId);
        return result;
    }

    public Map<String, Set<String>> getAllResRoleNames() {
        String sql = "select m.title, m.url, r.name from t_menu m " +
                "left join t_role_menu rm on m.id = rm.menu_id " +
                "left join t_role r on rm.role_id = r.id " +
                "where m.url is not null " +
                "order by url desc;";
        List<ResourceRoleResult> resRoles = jdbcTemplate.query(sql, new RowMapper<ResourceRoleResult>() {
            @Override
            public ResourceRoleResult mapRow(ResultSet rs, int rowNum) throws SQLException {
                return new ResourceRoleResult(rs.getString("url"), rs.getString("name"));
            }
        });
        Map<String, Set<String>> resRolesMap = new HashMap<>();
        resRoles.forEach(rrs -> {
            if (StringUtils.isBlank(rrs.getRoleName())) {
                return;
            }
            if (resRolesMap.containsKey(rrs.getUrl())) {
                resRolesMap.get(rrs.getUrl()).add(rrs.getRoleName());
            } else {
                Set<String> roleNames = new HashSet<>();
                roleNames.add(rrs.getRoleName());
                resRolesMap.put(rrs.getUrl(), roleNames);
            }
        });
        return resRolesMap;
    }
}
